package hbase;

/**
 * @author 鲁新茹
 * @date 2022/3/12
 */
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;


import java.io.IOException;
import java.util.List;

public class HbaseUtil {

    static final String p = "pdata";
    static final String m = "mdata";


    // 放置初始化方法
   public void initConfig() {
        Configuration conf = HBaseConfiguration.create();// 配置
        conf.set("hbase.zookeeper.quorum", "192.168.3.220");// zookeeper地址
        conf.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口
        conf.setInt("hbase.rpc.timeout", 20000);
        conf.setInt("hbase.client.operation.timeout", 20000);
        conf.setInt("hbase.client.scanner.timeout.period", 20000);
        conf.set("hbase.client.ipc.pool.type", "RoundRobinPool");
    }

    public  String  insertinfo(String ip,String port,String tableName,List<Student> list) throws Exception{
//        Connection con=getConnection(ip,port);

        Configuration conf =HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","192.168.3.220");
        HTable table=new HTable(conf,tableName);

//        HBaseAdmin admin = (HBaseAdmin)con.getAdmin();
//        Table table = con.getTable(TableName.valueOf(tableName));
//        boolean b = admin.tableExists(TableName.valueOf(tableName));
//        if(!b){
//            createTable(admin,tableName);
//        }
        insertList(table,list);
        return "添加成功";
    }


    //创建表的方法
    private  void createTable(Admin admin, String tableName) throws Exception {
        HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
        HColumnDescriptor descriptor2 = new HColumnDescriptor("pdata");
        HColumnDescriptor descriptor3 = new HColumnDescriptor("mdata");
        descriptor.addFamily(descriptor2);
        descriptor.addFamily(descriptor3);
        admin.createTable(descriptor);
    }
    //插入数据的方法
    private  void insertList(Table table, List<Student> list) throws Exception {
        for (Student student : list) {
            Put put = new Put(student.getStudentId().getBytes());
            put.addColumn(p.getBytes(), "name".getBytes(), student.getName().getBytes());
            put.addColumn(p.getBytes(), "date".getBytes(), student.getDate().getBytes());
            put.addColumn(p.getBytes(), "cn".getBytes(), student.getCn().getBytes());
            put.addColumn(p.getBytes(), "semester".getBytes(), student.getSemester().getBytes());
            put.addColumn(m.getBytes(), "studentStatus".getBytes(), student.getStudentStatus().getBytes());
            table.put(put);
        }
    }
    //获得与hbase的连接
    private  Connection getConnection(String ip, String port) throws Exception {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", ip);
        configuration.set("hbase.zookeeper.property.clientPort", port);
        Connection connection = ConnectionFactory.createConnection(configuration);

        return connection;
    }

//    public  void main(String[] args) {
//        ArrayList<Book> bookArrayList = new ArrayList<>();
//        Book book = new Book();
//        book.setBooksId("0001");
//
//        book.setName("omk");
//        bookArrayList.add(book);
//        try {
//            insertinfo("niit01","2181","Book",bookArrayList);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//
//    }

}
